package com.xiaoq.system.repository.impl;

import com.xiaoq.system.entity.ZoneMasterEntity;
import com.xiaoq.system.repository.IZoneMasterRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ZoneMasterRepositoryImpl implements IZoneMasterRepository {
	
	@Autowired
	private Datastore datastore;

	@Override
	public List<ZoneMasterEntity> findAllZoneMaster() {
		List<ZoneMasterEntity> zoneMasterList = datastore.createQuery(ZoneMasterEntity.class).asList();
		return zoneMasterList;
	}

	@Override
	public List<ZoneMasterEntity> find4HotActivities() {
		List<ZoneMasterEntity> zoneMasterList = datastore.createQuery(ZoneMasterEntity.class)
				.field("levels").equal(1)
				.field("onlineFlag").equal("1")
				.order("-hotScore")
				.asList();
		return zoneMasterList;
	}

	@Override
	public ZoneMasterEntity getZoneMasterByCode(String code) {
		if(StringUtils.isBlank(code)){
			return null;
		}
		return datastore.createQuery(ZoneMasterEntity.class).filter("code", code).get();
	}

	@Override
	public boolean save(ZoneMasterEntity zoneMasterEntity) {
		if(zoneMasterEntity!=null && StringUtils.isNotBlank(zoneMasterEntity.getId())){
			datastore.save(zoneMasterEntity);
			return true;
		}
		return false;
	}

	@Override
	public List<ZoneMasterEntity> findAllZoneMaster(String onlineFlag, Integer levels) {
		Query<ZoneMasterEntity> zoneMasterQuery = datastore.createQuery(ZoneMasterEntity.class);
		if(levels != null){
			zoneMasterQuery.filter("levels", levels);
		}else{
			List<Integer> levelss = new ArrayList<Integer>();
			levelss.add(1);
			levelss.add(2);
			zoneMasterQuery.filter("levels in", levelss);
		}
		if(onlineFlag != null){
			zoneMasterQuery.filter("onlineFlag", onlineFlag);
		}
		return zoneMasterQuery.asList();
	}

	@Override
	public List<ZoneMasterEntity> findAllZoneMasterByCityName(String cityName) {
		Query<ZoneMasterEntity> zoneMasterQuery = datastore.createQuery(ZoneMasterEntity.class);
		zoneMasterQuery.field("name").contains(cityName);
		return zoneMasterQuery.asList();
	}

	@Override
	public ZoneMasterEntity findZoneMasterByCityNameAndLevel(String cityName, Integer levels) {
		Query<ZoneMasterEntity> zoneMasterQuery = datastore.createQuery(ZoneMasterEntity.class);
		zoneMasterQuery.field("name").contains(cityName);
		zoneMasterQuery.field("levels").equal(levels);
		return zoneMasterQuery.asList().get(0);
	}

	@Override
	public List<ZoneMasterEntity> getZoneMasterListByCityCodeList(List<String> zoneCityCodeList) {
		if (CollectionUtils.isEmpty(zoneCityCodeList)) {
			return new ArrayList<>();
		}
		Query<ZoneMasterEntity> zoneMasterQuery = datastore.createQuery(ZoneMasterEntity.class)
				.filter("code in", zoneCityCodeList).field("areaCode").exists();
		return zoneMasterQuery.asList();
	}

	@Override
	public boolean updateById(ZoneMasterEntity zoneMasterEntity) {
		if(zoneMasterEntity == null || StringUtils.isEmpty(zoneMasterEntity.getId())){
			return false;
		}
		Query<ZoneMasterEntity> zoneMasterQuery = datastore.createQuery(ZoneMasterEntity.class)
				.filter("_id", zoneMasterEntity.getId());

		UpdateOperations<ZoneMasterEntity> updateOperations = datastore.createUpdateOperations(ZoneMasterEntity.class)
				.set("updatedAt", System.currentTimeMillis());

		if(zoneMasterEntity.getLatitude() != null){
			updateOperations.set("latitude", zoneMasterEntity.getLatitude());
		}
		if(zoneMasterEntity.getLongitude() != null){
			updateOperations.set("longitude", zoneMasterEntity.getLongitude());
		}
		datastore.update(zoneMasterQuery, updateOperations);
		return true;
	}

	@Override
	public List<ZoneMasterEntity> findAllZoneMaster(List<String> areaCodeList, Integer levels) {
		Query<ZoneMasterEntity> zoneMasterQuery = datastore.createQuery(ZoneMasterEntity.class);
		if(CollectionUtils.isNotEmpty(areaCodeList)) {
			zoneMasterQuery.filter("areaCode in", areaCodeList);
		}
		if(levels != null){
			zoneMasterQuery.filter("levels =", levels);
		}
		return zoneMasterQuery.asList();
	}
}
